/*
 * @lc app=leetcode id=621 lang=cpp
 *
 * [621] Task Scheduler
 */
class Solution {
public:
    int leastInterval(vector<char>& tasks, int n) {
        int sum = 0, total = 0;
        const int SIZE = 26;
        const int cycle = n+1;
        map<char, int> taskmap;
        for (auto c : tasks) {
            taskmap[c]++;
            total++;
        }

        int idx=0, count = 0;
        while(total>0) {
            idx = 0;
            map<char, bool> record;
            auto it = taskmap.begin();
            while(it!=taskmap.end()) {
                char c = it.first;
                if (taskmap[c] > 0) {
                    taskmap[c]--;
                    record[c] = true;
                    count++;
                    if (idx == cycle) {
                        break;
                    }
                } else {
                    taskmap.erase(c);
                }
            }
            total -= cycle;
        }
        
    }
};

